class BlackDeathWeapon extends Weapon
	abstract
	notplaceable;
		
var(Animations) name	WeaponEquipAnim;
var(Sounds) SoundCue 	WeaponEquipSnd;
var name FireAnim, AltFireAnim;

var int WeaponDamage;
var int HeavyWeaponDamage;

/** Holds the amount of Stamina used for a given shot */
var array<int> StaminaCost;

var Name HolderSocket;
var bool isHidden;

var EWeapAnimType WeapAnimType;

simulated function PostBeginPlay()
{
	super.PostBeginPlay();
	isHidden = false;
}

simulated function AttachWeaponTo(SkeletalMeshComponent MeshCpnt, optional Name SocketName )
{
	local BlackDeathPawn BDP;
	BDP = BlackDeathPawn(Instigator);
	//`log("attaching weapon");
	//Stick the weapon on!!!
	if (BDP.Mesh != None)
	//{
		// Attach Weapon mesh to player skelmesh
		if ( Mesh != None )
		{
			// Weapon Mesh Shadow
			Mesh.SetShadowParent(BDP.Mesh);
			Mesh.SetLightEnvironment(BDP.LightEnvironment);
			BDP.Mesh.AttachComponentToSocket(Mesh, SocketName);
		}

	//}
	//BDP.SetWeapAnimType(WeapAnimType);
	//GotoState('CurrentlyAttached');
}

simulated function DetachWeapon()//Note: consider deleteing DetachFrom and moving all detach functionality to here.
{
	DetachFrom(Instigator.Mesh);
}

simulated function DetachFrom( SkeletalMeshComponent MeshCpnt )
{
	//`log("detaching component:"@MeshCpnt);
	// Weapon Mesh Shadow
	if ( Mesh != None )
	{
		Mesh.SetShadowParent(None);
		Mesh.SetLightEnvironment(None);
		SetSkin(None);
	}
	if ( MeshCpnt != None )
	{
		// detach weapon mesh from player skelmesh
		if ( Mesh != None )
		{
			MeshCpnt.DetachComponent( mesh );
		}

		//BDPawn = BlackDeathPawn(Instigator);

		SetBase(None);
		SetHidden(True);
	}
}

simulated function ChangeVisibility(bool bIsVisible)
{
	`log("Changing visibility of"@self);
	if (Mesh != None)
	{
		Mesh.SetHidden(!bIsVisible);
		isHidden = !bIsVisible;
	}
}

simulated function TimeWeaponEquipping()
{
	AttachWeaponTo( Instigator.Mesh, BlackDeathPawn(Instigator).WeaponSocket);

	// Play the animation
	//PlayWeaponEquip();

	SetTimer( GetEquipTime() , false, 'WeaponEquipped');
}

simulated function PlayWeaponEquip()
{
	// Play the animation for the weapon being put down

	if ( WeaponEquipAnim != '' )
		PlayWeaponAnimation( WeaponEquipAnim, EquipTime );

	// play any assoicated sound
	if ( WeaponEquipSnd != None )
		WeaponPlaySound( WeaponEquipSnd );
}

simulated function float GetEquipTime()
{
	local float ETime;

	ETime = EquipTime>0 ? EquipTime : 0.01;
	if ( PendingFire(0) || PendingFire(1) )
	{
		ETime += 0.25;
	}
	return ETime;
}

simulated function WeaponPlaySound(SoundCue Sound, optional float NoiseLoudness)
{
	// if we are a listen server, just play the sound.  It will play locally
	// and be replicated to all other clients.
	if( Sound != None && Instigator != None )
	{
		Instigator.PlaySound(Sound, false, true);
	}
}
simulated function SetSkin(Material NewMaterial)
{
	local int i,Cnt;

	if ( NewMaterial == None )	// Clear the materials
	{
		if ( default.Mesh.Materials.Length > 0 )
		{
			Cnt = Default.Mesh.Materials.Length;
			for (i=0;i<Cnt;i++)
			{
				Mesh.SetMaterial( i, Default.Mesh.GetMaterial(i) );
			}
		}
		else if (Mesh.Materials.Length > 0)
		{
			Cnt = Mesh.Materials.Length;
			for ( i=0; i < Cnt; i++ )
			{
				Mesh.SetMaterial(i,none);
			}
		}
	}
	else
	{
		if ( default.Mesh.Materials.Length > 0 || mesh.GetNumElements() > 0 )
		{
			Cnt = default.Mesh.Materials.Length > 0 ? default.Mesh.Materials.Length : mesh.GetNumElements();
			for ( i=0; i < Cnt; i++ )
			{
				Mesh.SetMaterial(i,NewMaterial);
			}
		}
	}
}

simulated function FireAmmunition()
{
	CosumeStamina( CurrentFireMode );
	Super.FireAmmunition();
}

function CosumeStamina( byte FireModeNum )
{
	// Subtract the Ammo
	local BlackDeathHeroPawn BDHP;
	BDHP = BlackDeathHeroPawn(Instigator);
	if(BDHP != none)
	{
		BDHP.DecreaseStamina(StaminaCost[FireModeNum]);
	}
}
	
simulated state WeaponEquipping
{

	simulated event BeginState(Name PreviousStateName)
	{
		Super.BeginState(PreviousStateName);
		//BlackDeathPawn(Instigator).SwingBlend.SetBlendTarget(1.0,0.0);
		//BlackDeathPawn(Instigator).SwingVar.SetBlendTarget(0.0,0.0);
		BlackDeathPawn(Instigator).EquipAnim.PlayAnim(false,1.0,0.0);
	}
}

simulated state WeaponPuttingDown
{
	simulated event BeginState(Name PreviousStateName)
	{
		Super.BeginState(PreviousStateName);
		//BlackDeathPawn(Instigator).SwingBlend.SetBlendTarget(1.0,0.0);
		//BlackDeathPawn(Instigator).SwingVar.SetBlendTarget(1.0,0.0);
		BlackDeathPawn(Instigator).EquipAnim.PlayAnim(false,0.0,1.0);
	}
	simulated function WeaponIsDown()
	{
		Super.WeaponIsDown();
		//Instigator.invManager.NotifyEquipRemove(self);
		AttachToHolder(Instigator.Mesh, HolderSocket/*BlackDeathHeroPawn(Instigator).SwordHolderSocket*/);
	}
}

simulated function AttachToHolder(SkeletalMeshComponent MeshCpnt, optional Name SocketName )
{
	//`log("Attaching to holder Socket "@SocketName);
	AttachWeaponTo(MeshCpnt, SocketName);
}

function bool DenyPickupQuery(class<Inventory> ItemClass, Actor Pickup)
{
	return false;
}
	
DefaultProperties
{
	bHidden = false;
	WeaponEquipAnim=WeaponEquip
	HolderSocket = TestHolder
	
	StaminaCost(0)=5
	StaminaCost(1)=10
}